package com.clkj.cldisabledmanclient.mvp.recovertrainapply;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.base.BaseDialogFragment;
import com.clkj.cldisabledmanclient.eventbus.LinkmanEvent;
import com.clkj.cldisabledmanclient.util.Res;
import com.clkj.cldisabledmanclient.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by Administrator on 2016/8/2.
 */
public class DialogFragmentForAddLinker extends BaseDialogFragment implements View.OnClickListener{

    String mName;
    public static final String TITLE = "name";

    StrItemClickListener strItemClickListener;
    private LinearLayout mLyReply;
    private TextView mTvCancle;
    private TextView mTvEnsure;
    private EditText mEtLinkName;
    private EditText mEtLinkRelationship;
    private EditText mEtLinkUnit;
    private EditText mEtLinkPhone;


    public StrItemClickListener getStrItemClickListener() {
        return strItemClickListener;
    }

    public void setStrItemClickListener(StrItemClickListener strItemClickListener) {
        this.strItemClickListener = strItemClickListener;
    }

    public static DialogFragmentForAddLinker getInstance(String title) {
        DialogFragmentForAddLinker fragmentForAddLinker = new DialogFragmentForAddLinker();
        Bundle bundle = new Bundle();
        bundle.putString(TITLE, title);
        fragmentForAddLinker.setArguments(bundle);
        return fragmentForAddLinker;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            mName = bundle.getString(TITLE);
        }
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        //return inflater.inflate(R.layout.dialog_fragment_for_reply, container, false);

        final Window window = getDialog().getWindow();
        View view = inflater.inflate(R.layout.dialog_fragment_for_add_linker, ((ViewGroup) window.findViewById(android.R.id.content)), false);
        //需要用android.R.id.content这个view
        /*window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处*/

        //设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
        window.setBackgroundDrawable(new ColorDrawable(Res.getColor(R.color.grayqing)));
        WindowManager.LayoutParams windowAttributes = window.getAttributes();
        windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;//这个属性需要配合透明背景颜色,才会真正的 MATCH_PARENT
        // windowAttributes.height = DensityUtils.dp2px(getActivity(), 400);
        windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT

        //gravity
        windowAttributes.gravity = Gravity.CENTER;
        window.setAttributes(windowAttributes);

        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initView();
    }


    @Override
    public void initData() {

    }

    @Override
    public void assignView(View view) {
        mLyReply = view.findViewById(R.id.ly_reply);
        mTvCancle = view.findViewById(R.id.tv_cancle);
        mTvEnsure = view.findViewById(R.id.tv_ensure);
        mEtLinkName = view.findViewById(R.id.et_link_name);
        mEtLinkRelationship = view.findViewById(R.id.et_link_relationship);
        mEtLinkUnit = view.findViewById(R.id.et_link_unit);
        mEtLinkPhone = view.findViewById(R.id.et_link_phone);

    }

    @Override
    public void initView() {

        mTvCancle.setOnClickListener(this);
        mTvEnsure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_cancle:
                dismiss();
                break;

            case R.id.tv_ensure:

                if(TextUtils.isEmpty(mEtLinkName.getText().toString()))
                {
                    ToastUtil.showShort(getActivity(),"请输入姓名");
                    return;
                }
                if(TextUtils.isEmpty(mEtLinkRelationship.getText().toString()))
                {
                    ToastUtil.showShort(getActivity(),"请输入关系");
                    return;
                }
                if(TextUtils.isEmpty(mEtLinkUnit.getText().toString()))
                {
                    ToastUtil.showShort(getActivity(),"请输入所在单位");
                    return;
                }

                if(TextUtils.isEmpty(mEtLinkPhone.getText().toString()))
                {
                    ToastUtil.showShort(getActivity(),"请输入联系电话");
                    return;
                }

                EventBus.getDefault().post(new LinkmanEvent(mEtLinkName.getText().toString(),mEtLinkRelationship.getText().toString(),
                        mEtLinkUnit.getText().toString(),mEtLinkPhone.getText().toString()));

                dismiss();
                break;
        }
    }


    public interface StrItemClickListener {

        void onCheckStrItemClick(int status, String comment);
    }

}
